<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class obat_interaksi extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('m_obat_interaksi');
		$this->load->library('breadcrumb');
	}

	public function index(){
		$this->rule->type('R');
		$this->load->library('paging');

		// Menghapus session pencarian
		$this->session->unset_userdata('kelompok');
		$this->session->unset_userdata('nama');

		$config['sql'] = $this->m_obat_interaksi->get_all();
		$config['per_page'] = 6;
		$data = $this->paging->create_pagination($config);
		$judul = "Interaksi Obat";
		$data['judul'] = $judul;
		$this->layout->set_title($judul);
		$this->layout->set_desc("lorem ipsum");

		#breadcrumbs
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
		$this->breadcrumb->add_crumb('Obat', site_url('obat_kelompok#'));
		$this->breadcrumb->add_crumb($judul);

		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
		$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');
		$this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');


		//untuk motong kalimat
		$this->load->helper('trim_text');

		$this->layout->theme('apotek', 'obat_interaksi/index', $data);		
	}

	public function search(){
		$this->rule->type('R');

		if(isset($_POST['kelompok']) && isset($_POST['nama'])){
			$data['kelompok'] = $this->input->post('kelompok');
			$data['nama'] = $this->input->post('nama');
			$this->session->set_userdata('kelompok', $data['kelompok']);
			$this->session->set_userdata('nama', $data['nama']);

			#pagination
			$this->load->library('paging');
			$config['sql'] = $this->m_obat_interaksi->search($data);

			$config['per_page'] = 5;
			$data = $this->paging->create_pagination($config);

			$this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');

			if($this->session->userdata('nama') != ""){
				$judul = 'Pencarian interaksi obat "' . $this->session->userdata('nama') . '"';
				$data['judul']  = 'Pencarian interaksi obat " <font color="#FC771F">' . $this->session->userdata('nama') . '</font> "';
			}else{
				if($this->session->userdata('kelompok') != ""){
					$this->db->where('KdKelompok', $this->session->userdata('kelompok'));
					$q_kelompok = $this->db->get('kelompok');
					$res_kelompok  = $q_kelompok->result_array();
					$judul = 'Pencarian interaksi obat "' . $res_kelompok[0]['NmKelompok'] . '"';
					$data['judul']  = 'Pencarian interaksi obat " <font color="#FC771F">' . $res_kelompok[0]['NmKelompok'] . '</font> "';	
				}else{
					$judul = 'Pencarian interaksi obat " "';
					$data['judul']  = 'Pencarian interaksi obat " <font color="#FC771F"></font>"';	
				}				
			}

			$this->layout->set_title($judul);
			$this->layout->set_desc('Made with love by Raksa Abadi Informatika');

    	#import css + js
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
			$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');
			$this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');

			#breadcrumbs
			$this->breadcrumb->clear();
			$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
			$this->breadcrumb->add_crumb('Obat', site_url('obat_kelompok#'));
			$this->breadcrumb->add_crumb('Interaksi Obat', site_url('obat_interaksi'));
			$this->breadcrumb->add_crumb($judul);

			$this->load->helper('trim_text');

			$this->layout->theme('apotek','obat_interaksi/index', $data);
		}else{
			redirect('obat_interaksi', 'refresh');
		}
	}

	public function add(){
		$this->rule->type('C');

		if (isset($_POST) && !empty($_POST)) {
			$input  = $this->input->post();
			$this->m_obat_interaksi->store($input);

			redirect('obat_interaksi', 'refresh');
		}else{
			$judul = 'Tambah Data Interaksi Obat';
			$data['judul']  = $judul;

			$this->layout->set_title($judul);
			$this->layout->set_desc('Made with love by Raksa Abadi Informatika');

			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
			$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');

	    #breadcrumbs
			$this->breadcrumb->clear();
			$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
			$this->breadcrumb->add_crumb('Obat', site_url('obat_data#'));
			$this->breadcrumb->add_crumb('Interaksi Obat', site_url('obat_interaksi'));
			$this->breadcrumb->add_crumb($judul);

			$this->layout->theme('apotek','obat_interaksi/add', $data);
		}
	}

	public function edit($id){
		$this->rule->type('U');

		if (isset($_POST) && !empty($_POST)) {
			$input  = $this->input->post();
			$this->m_obat_interaksi->update($input);

			redirect('obat_interaksi', 'refresh');
		}else{
			$this->load->helper('cek_id');
			if(!cek_id($id, 'KdInteraksi', 'Interaksi')){
				show_404();
			}

			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
			$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');

			$judul = 'Edit Data Interaksi Obat';
			$data['judul'] = $judul;

			$this->layout->set_title($judul);
			$this->layout->set_desc('Made with love by Raksa Abadi Informatika');

  		#breadcrumbs
			$this->breadcrumb->clear();
			$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
			$this->breadcrumb->add_crumb('Obat', site_url('obat_kelompok#'));
			$this->breadcrumb->add_crumb('Interaksi Obat', site_url('obat_interaksi'));
			$this->breadcrumb->add_crumb($judul);

			$data['data'] = $this->m_obat_interaksi->get_by_id($id);

			$this->layout->theme('apotek','obat_interaksi/edit', $data);
		}
	}

	public function delete($id){
		$this->rule->type('D');

		$this->load->helper('cek_id');
		if(!cek_id($id, 'KdInteraksi', 'Interaksi')){
			set_flash('danger','Ups, Data interaksi obat tidak ditemukan');
			redirect('obat_interaksi','refresh');
		}

		$this->m_obat_interaksi->destroy($id);

		redirect($_SERVER['HTTP_REFERER'],'refresh');
	}

	public function excel($filter="", $keyword=""){
		$this->rule->type('R');
		
		#load our new PHPExcel library
		$this->load->library('excel');
		#activate worksheet number 1
		$this->excel->setActiveSheetIndex(0);
		#name the worksheet
		$this->excel->getActiveSheet()->setTitle('Data Interaksi Obat');
		#set cell A1 content with some text
		
		if($filter != ""){
			if($keyword != ""){
				#filter + keyword
				$this->excel->getActiveSheet()->setCellValue('A1', 'Interaksi Obat Dengan Kata Kunci "'.$keyword.'" '.site_title());
			}else{
				#filter
				$this->db->where('KdKelompok', $filter);
				$nm_filter = $this->db->get('kelompok')->result_array();
				$this->excel->getActiveSheet()->setCellValue('A1', 'Interaksi Obat Dengan Kata Kunci "'.$nm_filter[0]['NmKelompok'].'" '.site_title());
			}
		}else{
			if($keyword != ""){
				#keyword
				$this->excel->getActiveSheet()->setCellValue('A1', 'Interaksi Obat Dengan Kata Kunci "'.$keyword.'" '.site_title());
			}else{
				#semua data      
				$this->excel->getActiveSheet()->setCellValue('A1', 'Interaksi Obat '.site_title());	
			}			
		}

		$this->excel->getActiveSheet()->setCellValue('A2', 'No.');
		$this->excel->getActiveSheet()->setCellValue('B2', 'Kode Interaksi');
		$this->excel->getActiveSheet()->setCellValue('C2', 'Nama Obat 1');
		$this->excel->getActiveSheet()->setCellValue('D2', 'Nama Obat 2');
		$this->excel->getActiveSheet()->setCellValue('E2', 'Interkasi');
		#change the font size
		$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(11);
		#make the font become bold
		$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		#merge cell A1 until D1
		$this->excel->getActiveSheet()->mergeCells('A1:E1');
		#set aligment to center for that merged cell (A1 to D1)
		$this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		#set style
		$TitleStyle = array(
			'borders' => array(
				'allborders' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN
					)
				),
			'fill' => array(
				'type' => PHPExcel_Style_Fill::FILL_SOLID,
            //'color' => array('rgb' => 'FFFFFF') // hijau tua
				),
			'font'  => array(
				'bold'  => true,
				//'color' => array('rgb' => '#666666'),
				'size'  => 12,
				'name'  => 'calibri'
				)
			);

		$HeaderStyle = array(
			'borders' => array(
				'allborders' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN
					)
				),
			'fill' => array(
				'type' => PHPExcel_Style_Fill::FILL_SOLID,
            //'color' => array('rgb' => 'FFFFFF') // hijau
				),
			'font'  => array(
				'bold'  => true,
				'size'  => 12,
				'name'  => 'calibri'
				)
			);

		$ContentStyle = array(
			'borders' => array(
				'allborders' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN
					)
				),
			'fill' => array(
				'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => 'FFFFFF') // kuning
            ),
			'font'  => array(
				'size'  => 12,
				'name'  => 'calibri'
				)
			);

		$this->excel->getActiveSheet()->getStyle('A1:E1')->applyFromArray($TitleStyle);
		$this->excel->getActiveSheet()->getStyle('A2:E2')->applyFromArray($HeaderStyle);
		//isi
		if($filter == "" && $keyword == ""){
			$list_data = $this->m_obat_interaksi->export();	
		}else if($filter != "null" || $keyword != "" && $keyword != " "){
			$list_data = $this->m_obat_interaksi->export_by($filter, $keyword);
		}
		
		$cols = 3;
		$num = 1;
		foreach($list_data as $row){
			$col = $cols++;
			$this->excel->getActiveSheet()->setCellValue('A' . $col, $num++);
			$this->excel->getActiveSheet()->setCellValue('B' . $col, $row['KdInteraksi']);
			$this->excel->getActiveSheet()->setCellValue('C' . $col, $row['NmObat']);

			$this->db->join('obat', 'obat.KdObat = interaksi.KdObat2');
			$this->db->where('obat.KdObat', $row['KdObat2']);
			$q_obat2 = $this->db->get('interaksi');
			$obat2 = $q_obat2->result_array();

			$this->excel->getActiveSheet()->setCellValue('D' . $col, $obat2[0]['NmObat']);
			$this->excel->getActiveSheet()->setCellValue('E' . $col, $row['Interaksi']);
		}

		#warna
		
		$cols2 = 3;
		foreach($list_data as $row){
			$col2 = $cols2++;
			$this->excel->getActiveSheet()->getStyle('A' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('B' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('C' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('D' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('E' . $col2)->applyFromArray($ContentStyle);
		}


		$this->excel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("C")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("D")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("E")->setAutoSize(true);


		$filename='data interaksi obat.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache

		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');
	}
}

/* End of file obat_interaksi.php */
/* Location: ./application/controllers/obat_interaksi.php */